package com.vanke.kotlin_mvp

import com.vanke.kotlin_mvp.data.Task
import com.vanke.kotlin_mvp.data.TaskDataSource
import com.vanke.kotlin_mvp.data.TaskRepository
import com.vanke.kotlin_mvp.main.TasksFilterType

/**
 * @name Kotlin_MVP
 * @package name：com.vanke.kotlin_mvp
 * @class describe
 * @anthor shiyuchuan
 * @time 2017/8/16 11:11
 */
class MainPresenter(val tasksRepository: TaskRepository, val tasksView: MainContract.View) : MainContract.Presenter {

    override var currentFiltering: TasksFilterType = TasksFilterType.ALL_TASKS

    private var firstLoad = true

    init {
        tasksView.presenter = this
    }

    override fun start() {
        loadTasks(false)
    }

    override fun result(requestCode: Int, resultCode: Int) {

    }

    override fun loadTasks(forceUpdate: Boolean) {
        loadTasks(forceUpdate || firstLoad, true)
        firstLoad = false
    }

    private fun loadTasks(forceUpdate: Boolean, showLoadingUI: Boolean) {
        if (showLoadingUI) {
            tasksView.setLoadingIndicator(true)
        }
        if (forceUpdate) {
            tasksRepository.refreshTasks()
        }

        tasksRepository.getTasks(object : TaskDataSource.LoadTaskCallback {
            override fun onTaskLoaded(tasks: List<Task>) {
                val tasksToShow = ArrayList<Task>()
                for (task in tasks) {
                    when (currentFiltering) {
                        TasksFilterType.ALL_TASKS -> tasksToShow.add(task)
                        TasksFilterType.ACTIVE_TASKS -> if (task.isActive) {
                            tasksToShow.add(task)
                        }
                        TasksFilterType.COMPLETED_TASKS -> if (task.isCompleted) {
                            tasksToShow.add(task)
                        }
                    }
                }
                if (!tasksView.isActive) {
                    return
                }
                if (showLoadingUI) {
                    tasksView.setLoadingIndicator(false)
                }

                processTasks(tasksToShow)
            }

            override fun onDataNotAvailable() {
                if (!tasksView.isActive) {
                    return
                }
                tasksView.showLoadingTasksError()
            }

        })
    }

    private fun processTasks(tasksToShow: List<Task>) {
        if (tasksToShow.isEmpty()) {
            processEmptyTasks()
        } else {
            tasksView.showTasks(tasksToShow)
            showFilterLabel()
        }
    }

    private fun showFilterLabel() {
        when (currentFiltering) {
            TasksFilterType.ACTIVE_TASKS -> tasksView.showActiveFilterLable()
            TasksFilterType.COMPLETED_TASKS -> tasksView.showCompletedFilterLabel()
            else -> tasksView.showAllFilterLable()
        }
    }

    private fun processEmptyTasks() {
        when (currentFiltering) {
            TasksFilterType.ACTIVE_TASKS -> tasksView.showNoActiveTasks()
            TasksFilterType.COMPLETED_TASKS -> tasksView.showNoCompletedTasks()
            else -> tasksView.showNoTasks()
        }

    }

    override fun addNewTask() {
        tasksView.showAddTask()
    }

    override fun OpenTaskDetails(requestTask: Task) {
        tasksView.showTaskDetailsUi(requestTask.id)
    }

    override fun completeTask(completedTask: Task) {
        tasksRepository.completeTask(completedTask)
        tasksView.showTaskMarkedComplete()
        loadTasks(false, false)
    }

    override fun activateTask(activeTask: Task) {
        tasksRepository.activateTask(activeTask)
        tasksView.showTaskMarkedActive()
        loadTasks(false, false)
    }

    override fun clearCompletedTasks() {
        tasksRepository.clearCompletedTasks()
        tasksView.showCompletedTasksCleared()
        loadTasks(false, false)
    }

}